﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace LabMS.Web.Equipment.common
{
    public partial class buyappmodify : Base.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            strPageUserType.Add("teacher");

            if (Request.QueryString["ID"] != null)
            {
                if (!IsPostBack)
                {
                    int ID;
                    if (int.TryParse(Request.QueryString["ID"].ToString(), out ID))
                    {
                        try
                        {
                            IDHF.Value = ID.ToString();
                            LabMS.BLL.BuyApp buyAppBLL = new LabMS.BLL.BuyApp();
                            LabMS.Model.BuyApp buyAppModel = new LabMS.Model.BuyApp();

                            buyAppModel = buyAppBLL.GetModel(ID);
                            AppCommentTBX.Text = LabMS.Common.codehelper.Decode(buyAppModel.AppComment);//申请备注
                            NumbersTBX.Text = buyAppModel.ClassNumber;//分类号码
                            NameTBX.Text = buyAppModel.Name;//仪器名称
                            NumTBX.Text = buyAppModel.Numbers;//购买数量
                            TypeDDL.SelectedValue = buyAppModel.Type;//类型
                          
                        }
                        catch
                        {
                            ERRLbl.Text = "该购买申请不存在！";
                            ERRLbl.Visible = true;
                            SubmitBtn.Visible = false;
                            ModifyBtn.Visible = false;
                        }
                    }
                    else
                    {
                        ERRLbl.Text = "系统异常，参数不合法！";
                        ERRLbl.Visible = true;
                        SubmitBtn.Visible = false;
                        ModifyBtn.Visible = false;
                    }
                }
            }
            else
            {
                ERRLbl.Text = "系统异常，参数为空！";
                ERRLbl.Visible = true;
                SubmitBtn.Visible = false;
                ModifyBtn.Visible = false;
            }
        }

        protected void Modify_OnClick(object sender, EventArgs e)
        {
            #region 验证
            if (NumbersTBX.Text.Trim() == "")
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "分类号码不能为空!");
                return;
            }
            if (NumTBX.Text.Trim() == "")
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "购买数量不能为空!");
                return;
            }
            if (IDHF.Value == "")
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "参数丢失,请重新打开修改页面!");
                return;
            }
            #endregion
            LabMS.BLL.BuyApp buyAppBLL = new LabMS.BLL.BuyApp();
            LabMS.Model.BuyApp buyAppModel = new LabMS.Model.BuyApp();

            Button btn = (Button)sender;
            string strBtnID = btn.ID;
            string strAlert;

            buyAppModel.ClassNumber = NumbersTBX.Text.Trim();//分类号码
            buyAppModel.Name = NameTBX.Text.Trim();//编号名称
            if (NumbersTBX.Text.Trim() != "")
            {
                int iNum;
                if (int.TryParse(NumTBX.Text.Trim(), out iNum))
                {
                    buyAppModel.Numbers = iNum.ToString();//购买数量
                }
                else
                {
                    LabMS.Common.JShelper.JSAlert(Page, "err", "购买数量只能为不小于0整数!");
                    return;
                }
            }
            buyAppModel.Type = TypeDDL.SelectedValue;//类型
            if (strBtnID == "ModifyBtn")
            {
                buyAppModel.State = "0";//状态
                strAlert = "修改成功!";
            }
            else
            {
                buyAppModel.State = "1";//状态
                buyAppModel.AppTime = DateTime.Now;//申请时间
                strAlert = "申请成功!";
            }

            buyAppModel.AppComment = LabMS.Common.codehelper.Encode(AppCommentTBX.Text.Trim());//申请备注
            int ID;
            if (int.TryParse(IDHF.Value, out ID))
            {
                buyAppModel.ID = ID;
            }
            else
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "参数不合法,请重新打开修改页面!");
                return;
            }
            try
            {
                buyAppBLL.Update(buyAppModel);
                LabMS.Common.JShelper.JSAlertAndRedirect(Page, "success", strAlert, "buyappperview.aspx?ID=" + ID);
            }
            catch
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "操作失败!");
            }
        }
    }
}
